package com.api.gbhg.liuwenbo.api;

import com.alibaba.fastjson.JSON;
import com.api.gbhg.liuwenbo.dao.DataBaseUtil;
import com.api.gbhg.liuwenbo.dao.NonCentralTreeDBUtil;
import com.api.gbhg.liuwenbo.pojo.BudgetSectionIncome;
import weaver.conn.RecordSet;

import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import java.math.BigDecimal;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description 预算科收入预算表
 * @Author lwb
 * @Date 2021.12.2
 */
@Path("gb/BudgetSectionIncome")
public class BudgetSectionIncomeApi {

    private Map<String,Object> msgMap ;

    /**
     * 参数转换
     * @param param
     * @return
     */
    public Map<String,Object> paramFormat(Map<String,Object> param){
        String dw = param.get("dw").equals("")?DataBaseUtil.getGonBeiSubCompany(): param.get("dw").toString();
        Integer nf = param.get("nf").equals("")?LocalDate.now().getYear():Integer.parseInt(param.get("nf").toString());
        String jz = param.get("jz").equals("")?"yqndyjjzje":param.get("jz").toString();

        param.put("dw",dw);
        param.put("nf",nf);
        param.put("jz",jz);
        return param;
    }

    @POST
    @Path("/getBudgetIncomeList")
    @Produces(MediaType.APPLICATION_JSON)
    public String getBudgetIncomeList(Map<String,Object> param){
        msgMap = new HashMap<>();

        if (param == null ||!param.containsKey("nf") || !param.containsKey("dw") ||!param.containsKey("jz")){
            msgMap.put("code","500");
            msgMap.put("msg","缺少参数 请检查参数是否完整！");
            msgMap.put("data","");
            return JSON.toJSONString(msgMap);
        }

        Map<String, Object> paramFormat = paramFormat(param);

        List<BudgetSectionIncome> czbksrData = null;
        List<BudgetSectionIncome> fczbksrData = null;
        try {
            czbksrData = getCzbksrData(paramFormat);
            fczbksrData = getFczbksrData(paramFormat);
        }catch (Exception e){
            msgMap.put("code","500");
            msgMap.put("msg","程序异常，请联系管理员！");
            msgMap.put("data","");
            return JSON.toJSONString(msgMap);
        }

        Map<String,Object> map = new HashMap<>();
        map.put("1",czbksrData);
        map.put("2",fczbksrData);
        map.put("dw", DataBaseUtil.getSubcompanyNameById(paramFormat.get("dw").toString()));

        msgMap.put("code","200");
        msgMap.put("msg","");
        msgMap.put("data",map);
        return JSON.toJSONString(msgMap);
    }



    //------------------------------------------------------------------------------------------中央


    /**
     * 获取财政拨款收入数据
     * @param param
     * @return
     */
    public List<BudgetSectionIncome> getCzbksrData(Map<String,Object> param){

        List<BudgetSectionIncome> listBean = new ArrayList<>();

        String nf = param.get("nf").toString();
        String dw = param.get("dw").toString();
        String jz = param.get("jz").toString();

        RecordSet rs = new RecordSet();
        String sql = "select id,gnkmmc,kmdm,"+jz+",yjsr from uf_zyczbksr_km where srnf = "+nf+" and dw ="+dw+" ";
        System.err.println("获取财政拨款收入数据 "+sql);
        rs.executeQuery(sql);
        while (rs.next()){
            BudgetSectionIncome bean = new BudgetSectionIncome();
            bean.setKmmc(rs.getString("kmdm"));
            bean.setSnjz(rs.getString(jz));
            bean.setBnsr(rs.getString("yjsr"));
            bean.setHj(String.valueOf(new BigDecimal( rs.getString(jz)).add(new BigDecimal(rs.getString("yjsr")))));

            List<BudgetSectionIncome> xjList = getCzbksrEjData(rs.getString("id"), jz);
            if (xjList.size()>0)
                bean.setXj(xjList);

            bean.setName(rs.getString("gnkmmc"));
            listBean.add(bean);
        }
        return listBean;
    }

    /**
     * 获取中央科目二级数据
     * @param kmId
     * @return
     */
    public List<BudgetSectionIncome> getCzbksrEjData(String kmId,String jzField){
        RecordSet rs = new RecordSet();
        String sql = "select id,srzx,"+jzField+",yjsr from uf_zyczbksr_xj where szgnkm = "+kmId+" and sjzxsr is null";
        rs.executeQuery(sql);
        List<BudgetSectionIncome> list = new ArrayList<>();
        while (rs.next()){
            BudgetSectionIncome bean = new BudgetSectionIncome();
            bean.setKmmc(rs.getString("srzx"));
            bean.setSnjz(rs.getString(jzField));
            bean.setBnsr(rs.getString("yjsr"));
            bean.setHj(String.valueOf(new BigDecimal( rs.getString(jzField)).add(new BigDecimal(rs.getString("yjsr")))));

            List<BudgetSectionIncome> xjList = recursionXj(rs.getString("id"), jzField);
            if (xjList.size()>0)
                bean.setXj(xjList);

            list.add(bean);
        }
        return list;
    }

    /**
     * 获取所有下级信息
     * @param id
     * @param jzField
     * @return
     */
    public List<BudgetSectionIncome> recursionXj(String id,String jzField){
        RecordSet rs = new RecordSet();
        String sql = "select id,srzx,"+jzField+",yjsr from uf_zyczbksr_xj where sjzxsr = "+id+"";
        rs.executeQuery(sql);
        List<BudgetSectionIncome> list = new ArrayList<>();
        while (rs.next()){
            BudgetSectionIncome bean = new BudgetSectionIncome();
            bean.setKmmc(rs.getString("srzx"));
            bean.setSnjz(rs.getString(jzField));
            bean.setBnsr(rs.getString("yjsr"));
            bean.setHj(String.valueOf(new BigDecimal( rs.getString(jzField)).add(new BigDecimal(rs.getString("yjsr")))));

            List<BudgetSectionIncome> xjList = recursionXj(rs.getString("id"), jzField);
            if (xjList.size()>0)
                bean.setXj(xjList);

            list.add(bean);
        }
        return list;
    }

    //-----------------------------------------------------------------------------------------非中央

    /**
     * 获取非财政拨款收入数据
     * @param param
     * @return
     */
    public List<BudgetSectionIncome> getFczbksrData(Map<String,Object> param){
        List<BudgetSectionIncome> listBean = new ArrayList<>();

        Map<String, Object> srysfl = NonCentralTreeDBUtil.getSrysfl();

        String nf = param.get("nf").toString();
        String dw = param.get("dw").toString();
        String jz = param.get("jz").toString();

        RecordSet rs = new RecordSet();
        String sql = "select id,szfl,"+jz+",yjsr,sjsr from uf_fzysr where dw = "+dw+" and nf = "+nf+" ";
        rs.executeQuery(sql);
        while (rs.next()){
            BudgetSectionIncome bean = new BudgetSectionIncome();
            bean.setName(srysfl.get(rs.getString("szfl")).toString());

            List<BudgetSectionIncome> xjList = getFczbksrXm(rs.getString("id"), jz);
            if (xjList.size()>0)
                bean.setXj(xjList);

            listBean.add(bean);
        }
        return listBean;
    }

    /**
     * 获取非中央项目数据
     * @param id
     * @param jzField
     * @return
     */
    public List<BudgetSectionIncome> getFczbksrXm(String id,String jzField){
        RecordSet rs = new RecordSet();
        String sql = "select id,"+jzField+",yjsr,xmmc,sjsr from uf_fzysrxm where szfl = "+id+"";
        rs.executeQuery(sql);
        List<BudgetSectionIncome> list = new ArrayList<>();
        while (rs.next()){
            BudgetSectionIncome bean = new BudgetSectionIncome();
            bean.setKmmc(rs.getString("xmmc"));
            bean.setSnjz(rs.getString(jzField));
            bean.setBnsr(rs.getString("yjsr"));
            bean.setHj(String.valueOf(new BigDecimal( rs.getString(jzField)).add(new BigDecimal(rs.getString("yjsr")))));
            bean.setSjsr(rs.getString("sjsr"));//2023年4月11日 huangwm 增加实际收入返回
            list.add(bean);
        }
        return list;
    }

}
